package com.fingard.dsp.bank.directbank.hrbb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class HRBB01Balance extends HRBB01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try {
            //发银行报文的XML对象
            XmlTextWriter xmlWriter = new XmlTextWriter();
            HRBB01Header header = new HRBB01Header();
            header.bsnCode = "CBE001";
            header.serialNo = p_req7506.reqHeader.transSeqID;
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, header);
            xmlWriter.writeStartElement("ReqParam");
            while (p_req7506.readRecord()) {
                xmlWriter.writeElementString("accountNo", p_req7506.readedDetail.actNum);
            }
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                if (getRet(tmpStrRet[1])) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRets[2], tmpSbMsg);
                    if (tmpBkReader != null) {
                        while (tmpBkReader.read("/BankData/opRep/opResult/list/row")) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("accountNo");
                            tmpRetDetail.actCur = getBankConfig().getMapKeyByValue("curActMapping",tmpRetDetail.actNum);
                            tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("balance");
                            tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("balanceAvailable");
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                } else {
                    retAts.writeRespCode("-1");
                    if (tmpRets.length >= 3) {
                        retAts.writeRespInfo(tmpRets[1]);
                    } else {
                        retAts.writeRespInfo(tmpRets[0]);
                    }
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
